package com.boss.fileapp.dao.impl;

import com.boss.fileapp.consts.TableName;
import com.boss.fileapp.dao.IFileSendLogDao;
import com.boss.fileapp.entity.FileSendLog;
import com.boss.framework.dao.BaseDaoImpl;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Repository
public class FileSendLogDaoImpl extends BaseDaoImpl implements IFileSendLogDao {

    @Override
    public List<String> selectFileNameByProvince(String province,String sendBankCode) {
        List<String> params=new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        sb.append(" select ").append(" file_name ")
                .append(" from ").append(TableName.FILE_SEND_LOG)
                .append(" where ").append(" is_send = 0 and mof_div_code = ? ");
        params.add(province);
        if(!StringUtils.isEmpty(sendBankCode)){
            sb.append(" and send_bank_code=? ");
            params.add(sendBankCode);
        }
        return super.getjdbcTemplate().queryForList(sb.toString(), String.class, params.toArray());
    }

    @Override
    public List<FileSendLog> selectByConditions(Map<String, Object> conditions) {
        StringBuilder sb = new StringBuilder();
        List<Object> params = new ArrayList<>();
        sb.append(" select ")
                .append(" id, update_time, create_time, ")
                .append(" file_name, is_send, send_time, ")
                .append(" send_bank_code, file_action_type ")
                .append(" from ")
                .append(TableName.FILE_SEND_LOG);
        if (!CollectionUtils.isEmpty(conditions)) {
            sb.append(" where 1=1 ");
            if (!StringUtils.isEmpty(conditions.get("id"))) {
                sb.append(" and id = ? ");
                params.add(conditions.get("id"));
            }
            if (!StringUtils.isEmpty(conditions.get("updateTime"))) {
                sb.append(" and update_time = ? ");
                params.add(conditions.get("updateTime"));
            }
            if (!StringUtils.isEmpty(conditions.get("createTime"))) {
                sb.append(" and create_time = ? ");
                params.add(conditions.get("createTime"));
            }
            if (!StringUtils.isEmpty(conditions.get("fileName"))) {
                sb.append(" and file_name like concat('%', ?, '%') ");
                params.add(conditions.get("fileName"));
            }
            if (!StringUtils.isEmpty(conditions.get("isSend"))) {
                sb.append(" and is_send = ? ");
                params.add(conditions.get("isSend"));
            }
            if (!StringUtils.isEmpty(conditions.get("sendTime"))) {
                sb.append(" and send_time = ? ");
                params.add(conditions.get("sendTime"));
            }
            if (!StringUtils.isEmpty(conditions.get("sendBankCode"))) {
                sb.append(" and send_bank_code = ? ");
                params.add(conditions.get("sendBankCode"));
            }
            if (!StringUtils.isEmpty(conditions.get("fileActionType"))) {
                sb.append(" and file_action_type = ? ");
                params.add(conditions.get("fileActionType"));
            }
            if (!StringUtils.isEmpty(conditions.get("mofDivCode"))) {
                sb.append(" and mof_div_code = ? ");
                params.add(conditions.get("mofDivCode"));
            }
        }
        return super.getjdbcTemplate().query(sb.toString(), params.toArray(), new BeanPropertyRowMapper<>(FileSendLog.class));
    }

    @Override
    public void batchInsertFileSendLog(List<FileSendLog> fileSendLogs) {
        StringBuilder sb = new StringBuilder();
        List<Object[]> params = new ArrayList<>();
        sb.append(" insert into ")
                .append(TableName.FILE_SEND_LOG)
                .append(" (id, update_time, create_time, file_name, ")
                .append(" is_send, send_bank_code, file_action_type, mof_div_code) ")
                .append(" values ")
                .append(" (?, ?, ?, ?, ?, ?, ?, ?) ");
        for (FileSendLog fileSendLog : fileSendLogs) {
//
//            if (fileSendLogs.indexOf(fileSendLog) != fileSendLogs.size() - 1) {
//                sb.append(" , ");
//            }
            Object[] paramArr = {
                    fileSendLog.getId(),
                    fileSendLog.getUpdateTime(),
                    fileSendLog.getCreateTime(),
                    fileSendLog.getFileName(),
                    fileSendLog.getIsSend(),
                    fileSendLog.getSendBankCode(),
                    fileSendLog.getFileActionType(),
                    fileSendLog.getMofDivCode()
            };
            //params.addAll(Arrays.asList(paramArr));
            params.add(paramArr);
        }
        this.getJdbcTemplate().batchUpdate(sb.toString(), params);
        //super.getjdbcTemplate().update(sb.toString(), params.toArray());
    }

    @Override
    public void updateFileSendLog(FileSendLog fileSendLog) {
        StringBuilder sb = new StringBuilder();
        sb.append(" update ")
                .append(TableName.FILE_SEND_LOG)
                .append(" set ")
                .append(" update_time = ?, is_send = ?, send_time = ? ")
                .append(" where ")
                .append(" file_name = ? ");
        Object[] params = {
                fileSendLog.getUpdateTime(),
                fileSendLog.getIsSend(),
                fileSendLog.getSendTime(),
                fileSendLog.getFileName()
        };
        super.getjdbcTemplate().update(sb.toString(), params);
    }

    @Override
    public void physicalDeleteHistoryLog(String province,String sendBankCode) {
        List<String> params=new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        sb.append(" delete from ").append(TableName.FILE_SEND_LOG)
                .append(" where ").append(" is_send = 0 and mof_div_code = ? ");
        params.add(province);
        if(!StringUtils.isEmpty(sendBankCode)){
            sb.append(" and send_bank_code=? ");
            params.add(sendBankCode);
        }
        super.getjdbcTemplate().update(sb.toString(), params.toArray());
    }
}
